package factory;

import java.util.HashMap;
import java.util.Map;

import base.BaseFragment;

import android.support.v4.app.Fragment;
import fragment.AppFragment;
import fragment.CategoryFragment;
import fragment.GameFragment;
import fragment.HomeFragment;
import fragment.HotFragment;
import fragment.RecommendFragment;
import fragment.SubjectFragment;

/**
 * Fragment 的工厂
 * @author Anson-pc
 *
 */
public class FragmentFactory {
	 public static final int FRAGMENT_HOME = 0;
	    public static final int FRAGMENT_APP = 1;
	    public static final int FRAGMENT_GAME = 2;
	    public static final int FRAGMENT_SUBJECT = 3;
	    public static final int FRAGMENT_RECOMMEND = 4;
	    public static final int FRAGMENT_CATEGORY = 5;
	    public static final int FRAGMENT_HOT = 6;

	    //创建一个Map集合
	    private static Map<Integer,BaseFragment> mCacheFragment = new HashMap<Integer,BaseFragment>();
	    
	    
	    public static BaseFragment creatFragment(int index){
	        BaseFragment fragment = null;
	        
	        //判断集合中是否存在fragment
	        if (mCacheFragment.containsKey(index)) {
				fragment = mCacheFragment.get(index);
				return fragment;
			}
	        switch (index){
	            case FRAGMENT_HOME:
	                fragment = new HomeFragment();
	                break;
	            case FRAGMENT_APP:
	                fragment = new AppFragment();
	                break;
	            case FRAGMENT_GAME:
	                fragment = new GameFragment();
	                break;
	            case FRAGMENT_SUBJECT:
	                fragment = new SubjectFragment();
	                break;
	            case FRAGMENT_RECOMMEND:
	                fragment = new RecommendFragment();
	                break;
	            case FRAGMENT_CATEGORY:
	                fragment = new CategoryFragment();
	                break;
	            case FRAGMENT_HOT:
	                fragment = new HotFragment();
	                break;
	            default:
	                break;
	        }
	        //保存fragment到Map中
	        mCacheFragment.put(index, fragment);
	        return fragment;
	    }
}
